/*
 * 题库
 */

import { createAPI } from '@/utils/request'
const formOfEmploymentMapping = new Map([
  ['1', '单选题'],
  ['2', '多选题'],
  ['3', '简答题']
])
const formOfDifficultyMapping = new Map([
  ['1', '简单'],
  ['2', '一般'],
  ['3', '困难']
])
const formOfChkStatesMapping = new Map([
  [0, false],
  [1, false],
  [2, true]
])

// 基础题库列表
export const list = data => createAPI('/questions', 'get', data)
  .then(res => ({
    ...res.data,
    countsInfo: `数据一共 ${res.data.counts} 条`,
    items: res.data.items.map(i => ({
      ...i,
      questionType: formOfEmploymentMapping.get(i.questionType),
      difficulty: formOfDifficultyMapping.get(i.difficulty)
    }))
  }))
  .catch(() => [])

// 基础题库详情
export const detail = data => createAPI(`/questions/${data.id}`, 'get', data)
  .then(res => ({
    ...res.data,
    questionType: formOfEmploymentMapping.get(res.data.questionType),
    difficulty: formOfDifficultyMapping.get(res.data.difficulty)
  }))
  .catch(() => ({}))

// 批量导入题
export const batch = data => createAPI('/questions/batch', 'post', data)

// 基础题库添加
export const add = data => createAPI('/questions', 'post', data)

// 基础题库修改
export const update = data => createAPI(`/questions/${data.id}`, 'put', data)

// 基础题库删除
export const remove = data => createAPI(`/questions/${data.id}`, 'delete', data)

// 精选题库加入取消
export const choiceAdd = data =>
  createAPI(`/questions/choice/${data.id}/${data.choiceState}`, 'patch', data)

// 精选题库列表
export const choice = data => createAPI('/questions/choice', 'get', data)
  .then(res => ({
    ...res.data,
    countsInfo: `数据一共 ${res.data.counts} 条`,
    items: res.data.items.map(i => ({
      ...i,
      questionType: formOfEmploymentMapping.get(i.questionType),
      difficulty: formOfDifficultyMapping.get(i.difficulty),
      chkStates: formOfChkStatesMapping.get(i.chkState)
    }))
  }))
  .catch(() => [])

// 精选题库上下架
export const choicePublish = data => createAPI(`/questions/choice/${data.id}/${data.publishState}`, 'post', data)

// 试题审核
export const choiceCheck = data => createAPI(`/questions/check/${data.id}`, 'post', data)

// 试题审核意见列表
export const auditOpinions = data => createAPI(`/questions/${data.id}/auditOpinions`, 'get', data)

// 出题记录
export const setRecords = data =>
  createAPI(`/questions/${data.id}/setRecords`, 'get', data)

// 组题列表
export const randoms = data => createAPI('/questions/randoms', 'get', data)

// 组题列表删除
export const removeRandoms = data =>
  createAPI(`/questions/randoms/${data.id}`, 'delete', data)
